מבני נתונים 08a תרגול 8 14/2/2008 המשך ערמות ליאור שפירא
ערמות פיבונאצ'י Operation Linked List Binary Heap Binomial Heap Fibonacci Heap Relaxed Heap make-heap 1 1 1 1 1 is-empty 1 1 1 1 1 insert 1 log n log n 1 1 delete- n log n log n log n log n decrease-key n log n log n 1 1 delete n log n log n log n log n union 1 n log n 1 1 find- n 1 log n 1 1 n = number of elements in priority queue amortized
ערימות פיבונאצ'י - מבנה Fibonacci heap. each parent larger than its children Set of heap-ordered trees. Maintain pointer to imum element. Set of marked nodes. roots heap-ordered tree 1 24 3 26 46 18 52 Heap H 3 35 44
ערימות פיבונאצ'י - מבנה Fibonacci heap. Set of heap-ordered trees. Maintain pointer to imum element. Set of marked nodes. find- takes O(1) time 1 24 3 26 46 18 52 Heap H 4 35 44
ערימות פיבונאצ'י - מבנה Fibonacci heap. Set of heap-ordered trees. Maintain pointer to imum element. Set of marked nodes. use to keep heaps flat (stay tuned) 1 24 3 26 46 18 52 Heap H 5 35 marked 44
Cascading cuts & Successive linking פעולת delete- יודעים מי המינימום בערימה נתלה את הבנים שלו כעצים בערימה מכל דרגה עץ יחיד! נבצע successive linking 24 1 3 26 46 18 52 35 44
Cascading cuts & Successive linking פעולת delete- יודעים מי המינימום בערימה נתלה את הבנים שלו כעצים בערימה מכל דרגה עץ יחיד! נבצע successive linking 24 1 18 52 26 46 44 35
Cascading cuts & Successive linking פעולת delete- יודעים מי המינימום בערימה נתלה את הבנים שלו כעצים בערימה מכל דרגה עץ יחיד! נבצע successive linking עלות הפעולה: Amortized O(rank(H)) 52 18 24 1 26 46 44 35
Cascading cuts & Successive linking decrease-key פעולת אינטואיציה: אם חוק הערמה לא מופר, פשוט נשנה את ערך הצומת אחרת נחתוך את תת העץ של הצומת ונתלה כעץ חדש בכדי לשמור על עצים שטוחים יחסית, ברגע שחותכים בן שני לצומת מסוים, גם הוא נתלה כעץ חדש 18 38 marked node: one child already cut 24 1 21 26 46 52 35 88 2
Fibonacci Heaps: Decrease Key Case 1. [heap order not violated] Decrease key of x. Change heap pointer (if necessary). 18 38 24 1 21 26 46 29 52 x 10 35 88 2 decrease-key of x from 46 to 29
Fibonacci Heaps: Decrease Key Case 1. [heap order not violated] Decrease key of x. Change heap pointer (if necessary). 18 38 24 1 21 26 29 52 x 11 35 88 2 decrease-key of x from 46 to 29
Fibonacci Heaps: Decrease Key Case 2a. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). 18 38 24 1 21 p 26 29 15 52 x 12 35 88 2 decrease-key of x from 29 to 15
Fibonacci Heaps: Decrease Key Case 2a. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). 18 38 24 1 21 p 26 15 52 x 13 35 88 2 decrease-key of x from 29 to 15
Fibonacci Heaps: Decrease Key Case 2a. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). x 15 18 38 2 24 1 p 21 26 52 14 35 88 decrease-key of x from 29 to 15
Fibonacci Heaps: Decrease Key Case 2a. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). x 15 18 38 2 mark parent 24 1 p 21 26 52 15 35 88 decrease-key of x from 29 to 15
Fibonacci Heaps: Decrease Key Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). 15 18 38 2 24 1 21 p 26 52 16 x 35 5 88 decrease-key of x from 35 to 5
Fibonacci Heaps: Decrease Key Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). 15 18 38 2 24 1 21 p 26 52 1 x 5 88 decrease-key of x from 35 to 5
Fibonacci Heaps: Decrease Key Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). 15 x 5 18 38 2 24 1 21 p 26 52 18 88 decrease-key of x from 35 to 5
Fibonacci Heaps: Decrease Key Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). 15 x 5 18 38 2 second child cut 24 1 21 p 26 52 19 88 decrease-key of x from 35 to 5
Fibonacci Heaps: Decrease Key Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). 15 x 5 p 26 18 38 2 88 24 1 21 52 20 decrease-key of x from 35 to 5
Fibonacci Heaps: Decrease Key Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). 15 x 5 p 26 18 38 2 88 24 p' 1 21 second child cut 52 21 decrease-key of x from 35 to 5
Fibonacci Heaps: Decrease Key Case 2b. [heap order violated] Decrease key of x. Cut tree rooted at x, meld into root list, and unmark. If parent p of x is unmarked (hasn't yet lost a child), mark it; Otherwise, cut p, meld into root list, and unmark (and do so recursively for all ancestors that lose a second child). x p p' 15 5 26 24 p'' 18 38 2 88 don't mark parent if it's a root 1 21 52 22 decrease-key of x from 35 to 5
ערמות פיבונצ'י כמו שראיתם בכיתה: Rank(H)<log Ф (n)
שאלה משבוע שעבר בהינתן מימוש של Fibonacci heaps בו לא מתבצע,cascading cuts הראו שעבור סדרת m פעולות על מקס' n אברים, עלות פעולה ממוצעת גבוהה ככל האפשר קצת תזכורת פעולות decrease-key מאד מהירות בעת פעולת extract- )זמן קבוע( נבצע consolidate דרגת כל צומת הינה D(n) והיא חסומה ע"י O(logn)
שאלה משבוע שעבר בהינתן שאין,cascading cuts מה משתנה ב"מה שמובטח לנו"? ז.א איפה השתמשנו בידע שלא ניתן להוריד 2 בנים מצומת בלי שהוא ייחתך? בהוכחה שהדרגה המקסימלית O(logn) )חסם על )D(n) איפה משתמשים בעובדה זו? ב- extract נעבור על כל השורשים )מקס' )D(n) ז"א אם נגיע למצב שבו יש הרבה מאד עצים בערמה, כל פעולה תהיה יקרה מעבר לכך, אם כל עץ מדרגה שונה, פעולת ה- consolidate לא תחבר עצים ביחד
שאלה משבוע שעבר כיצד נגדיר עץ מדרגה k שהוא "ממש גרוע"? נגדיר עץ מיוחד מדרגה.k-rank star k זהו שורש עם k בנים, כולם עלים. K=2 K=1 K=8
שאלה משבוע שעבר הפתרון יורכב משני שלבים.1.2 בסדרה של f(n) שלבים, בנו ערמת פיבונצ'י כך שיש 1 rank-0 star 1 rank-1 star 1 rank-sqrt(n) star חזרו על הפעולות הבאות מספר רב של פעמים )O(m)( הכנס ערך X ממש קטן בצע extract- )מוחק את X( כל אחת מהפעולות לוקחת Ω( n) זמן, כך שעבור m>>n פעולה תיקח בממוצע n
שאלה משבוע שעבר אך כיצד בונים?k-rank star כיצד נוודא שבשום שלב לא יהיו יותר מ- n אברים בערמה? פתרון )באינדוקציה( K 2 K elements 2 -(k-1) elements k 2 -(k-1)>(k-1) 2 K-1 rank star... K-1 rank star...
שאלה משבוע שעבר אך כיצד בונים?k-rank star כיצד נוודא שבשום שלב לא יהיו יותר מ- n אברים בערמה? פתרון K-1 rank star K-1 rank star...... K rank star...
ערמות פיבונאצ'י תרגיל 2 האם ניתן לבנות ערמת פיבונאצ'י ובה עץ אחד מעומק n? פתרון עבור n=1 5 9 עבור 2=n Extract- 9
ערמות פיבונאצ'י תרגיל 2 האם ניתן לבנות ערמת פיבונאצ'י ובה עץ אחד מעומק n? key[ z '] k ey[ y '] key[ x '] [ H] פתרון נניח שאנו יודעים לפתור עבור n=k נפתור עבור n=k+1 נגדיר x,y,z כך ש- נכניס אותם לתוך הערמה 5 9 20 25 35
ערמות פיבונאצ'י תרגיל 2 האם ניתן לבנות ערמת פיבונאצ'י ובה עץ אחד מעומק n? key[ z '] k ey[ y '] key[ x '] [ H] 5 9 20 פתרון נניח שאנו יודעים לפתור עבור n=k נפתור עבור n=k+1 נגדיר x,y,z כך ש- נכניס אותם לתוך הערמה נפעיל extract- 25 35
ערמות פיבונאצ'י תרגיל 2 האם ניתן לבנות ערמת פיבונאצ'י ובה עץ אחד מעומק n? key[ z '] k ey[ y '] key[ x '] [ H] 20 פתרון נניח שאנו יודעים לפתור עבור n=k נפתור עבור n=k+1 נגדיר x,y,z כך ש- נכניס אותם לתוך הערמה נפעיל extract- 9 25 35
ערמות פיבונאצ'י תרגיל 2 האם ניתן לבנות ערמת פיבונאצ'י ובה עץ אחד מעומק n? key[ z '] k ey[ y '] key[ x '] [ H] 9 20 פתרון נניח שאנו יודעים לפתור עבור n=k נפתור עבור n=k+1 נגדיר x,y,z כך ש- נכניס אותם לתוך הערמה נפעיל extract- )9( נמחק את x 25 35
תרגיל 3 בערמות פיבונצ'י, אנחנו מבצעים cascading cuts בצומת v אם הוא איבד צומת בן מאז הפעם האחרונה שהוא נתלה על צומת אחרת. נניח שנבצע CC רק אם v איבד שני בנים מאז, כיצד משתנה הלמה: x צומת בערמת פיב', y 1 y,, n בנים של x, מסודרים לפי הסדר בו נתלו על i. לכל rank(y i ישן ועד הכי חדש(. אזי 2-i ( )הכי x
Answer rank(y i ) i-3 Since y i had the same rank as x when it became a child of x תרגיל 3 x must have had at least i-1 children at that time, so y i had at least i-1 rank. It could have lost at most two children since then, therefore rank at least i-3
הסוף